Plotly

  • plotly is a high-level interface to plotly.js, based on d3.js which provides an easy-to-use UI to generate slick D3 interactive graphics. These interactive graphs give the user the ability to zoom the plot in and out, hover over a point to get additional information, filter to groups of points, and much more. These interactive components contribute to an engaging user experience and allows information to be displayed in ways that are not possible with static figures.

  • plotly is a web application for creating and sharing data visualizations. Plotly can work with several programming languages and applications including R, Python, and Microsoft Excel. We’re going to concentrate on creating different graphs with Plotly.

  • Interactive Graphics!

    • Zooming
    • Silencing
    • Hovering
    • Sliding, etc.
  • If you want to share your visualizations on https://plot.ly/ you should make an account on their site.

How to create a plotly object

  • There are two main approaches to initialize a plotly object:

    • transforming a ggplot2 object with ggplotly()
    • setting up aesthetics mappings with plot_ly() directly

Web-based ggplot2 graphics: ggplotly

  • ggplotly() takes existing ggplot2 objects and converts them into interactive plotly graphics. That is, ggplotly() converts your static plots to an interactive web-based version!

  • This makes it easy to create interactive figures because we are already familiar to the ggplot2 syntax.

Bar Plot

library(plotly)
g <- ggplot(data=incex) + geom_bar(mapping = aes(x = occ))

ggplotly(g)

Histogram

g2 <- ggplot(incex) + geom_histogram(aes(x = income), bins = 20) + theme_minimal()

ggplotly(g2)

Density Plot

g3 <- ggplot(incex) + geom_density(aes(x = income)) + theme_minimal()

ggplotly(g3)

Scatterplot

g4 <- ggplot(incex, aes(x = age, y = income)) + geom_point() + geom_smooth(method="loess", formula=y~x, se=F) + theme_bw()

ggplotly(g4)

Using plotly without ggplot2

  • plot_ly() is the base plotly command to initialize a plot from a dataframe, similar to ggplot() from ggplot2.

Bar plot

occ_counted <- incex %>% count(occ, name = 'count') 
occ_counted
   occ count
1  low   595
2 med.   700
3 high   627
plot_ly(occ_counted, x= ~occ, y=~count, type = "bar")
  • Add hover texts to a bar plot, and produce red bars with black outline.
occ_counted$text <- c("low level", "medium level", "high level")

p<- plot_ly(occ_counted, x= ~occ, y=~count, type = "bar", 
             text = ~text, 
              color = I("skyblue"), 
              stroke = I("red"), 
              span = I(10))
p
# change title, xlabel, and ylabel
p %>% layout(title = "Freq. of Occupation Status",
         xaxis = list(title = "Occupation Status"),
         yaxis = list(title = "Frequency"))

Boxplot

plot_ly(incex, y = ~income, type = "box", name = '')
  • create grouped boxplots.
?airmiles
plot_ly(incex, y = ~income, color = ~occ, type = "box")

Histogram

plot_ly(incex, x = ~income, type = "histogram")

Scatterplot

plot_ly(incex, x = ~income, y = ~age, type = "scatter", mode = "markers")

Scatterplot with different colors

  • Add color to your scatterplot points according to a categorical variable in the data frame you use with plot_ly().
plot_ly(incex, x = ~income, y = ~age, type = "scatter", mode = "markers",
        color = ~edu)
  • You may want to change a color brewer palette.
plot_ly(incex, x = ~income, y = ~age, type = "scatter", mode = "markers",
        color = ~edu, colors = "Set1")

Scatterplot with different symbols

  • Create a subset of incex data. Change symbols for educational status.
set.seed(1)
index <- sample(1:nrow(incex), 100, replace=FALSE)

subincex <- incex[index, ]
plot_ly(subincex, x = ~income, y = ~age, type = "scatter", mode = "markers",
        symbol = ~edu, 
        symbols = c('circle','x','o'), 
        color = I('black'), 
        marker = list(size = 10))

3D Scatterplot

  • Create a three-dimensional scatterplot with the type = "scatter3d" argument. If you click and drag these scatterplots, you can view them from different angles.
plot_ly(subincex, x = ~income, y = ~age, z= ~oexp, type = "scatter3d", mode = "markers", color = ~oexp, name="")

Lineplot

data("airmiles") # load airmiles data
str(airmiles) # check structure
 Time-Series [1:24] from 1937 to 1960: 412 480 683 1052 1385 ...
plot_ly(x = time(airmiles), y = airmiles, type = 'scatter', mode = 'lines')